/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.usc.cs576.lib.xml;

import edu.usc.cs576.lib.Action;
import edu.usc.cs576.lib.mediaObject.Audio;
import edu.usc.cs576.lib.mediaObject.Image;
import edu.usc.cs576.lib.mediaObject.KeyFrame;
import edu.usc.cs576.lib.mediaObject.Track;
import edu.usc.cs576.lib.mediaObject.Video;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.w3c.dom.*;
import javax.xml.parsers.*;
import java.io.*;
import org.xml.sax.SAXException;

/**
 *
 * @author haoxu
 */
public class XMLReader {

    String filename;
    String absolutePath;
    Document doc;

    public XMLReader(String filename, String absolutePath) {
        this.filename = filename;
        this.absolutePath = absolutePath;
    }

    public void init() {
        try {
            File file = new File(absolutePath);
            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
            DocumentBuilder db = dbf.newDocumentBuilder();
            doc = db.parse(file);
        } catch (SAXException ex) {
            Logger.getLogger(XMLReader.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(XMLReader.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ParserConfigurationException ex) {
            Logger.getLogger(XMLReader.class.getName()).log(Level.SEVERE, null, ex);
        }

    }

    public int getWidth() {
        Element e = doc.getDocumentElement();
        int width = getIntValue(e, "canvasWidth");
        return width;
    }

    public int getHeight() {
        Element e = doc.getDocumentElement();
        int height = getIntValue(e, "canvasHeight");
        return height;
    }

    public ArrayList<Track> getTracks() {
        ArrayList<Track> tracks = new ArrayList<Track>();

        NodeList nodes = doc.getElementsByTagName("track");
        for (int i = 0; i < nodes.getLength(); i++) {
            Track track = new Track();

            Element e = (Element) nodes.item(i);
            String name = getTextValue(e, "name");
            int width = getIntValue(e, "width");
            int height = getIntValue(e, "height");

            NodeList actionsFather = e.getElementsByTagName("actions");
            if (actionsFather != null && actionsFather.getLength() > 0) {
                Element e2 = (Element) actionsFather.item(0);
                NodeList actions = e2.getElementsByTagName("action");
                if (actions != null && actions.getLength() > 0) {
                    for (int j = 0; j < actions.getLength(); j++) {
                        Action action = new Action();
                        Element ea = (Element) actions.item(j);
                        int actionNumber = getIntValue(ea, "actionNum");
                        action.setActionNumber(actionNumber);
                        String param;
                        String param2;
                        switch (actionNumber) {
                            case 7:
                                param = getTextValue(ea, "param1");
                                action.setParam1(param);
                                break;
                            case 8:
                                param = getTextValue(ea, "param1");
                                param2 = getTextValue(ea, "param2");
                                action.setParam1(param);
                                action.setParam2(param2);
                                track.setStartFrame(Integer.parseInt(param));
                                track.setEndFrame(Integer.parseInt(param2));
                                break;
                            default:
                        }
                        track.getActions().add(action);
                    }
                }
            }

            NodeList keyframeFather = e.getElementsByTagName("keyFrames");
            if (keyframeFather != null && keyframeFather.getLength() > 0) {
                Element e3 = (Element) keyframeFather.item(0);
                NodeList keyframes = e3.getElementsByTagName("keyframe");
                if (keyframes != null && keyframes.getLength() > 0) {
                    for (int j = 0; j < keyframes.getLength(); j++) {
                        KeyFrame frame = new KeyFrame();
                        Element ea = (Element) keyframes.item(j);
                        frame.setFrameNum(getIntValue(ea, "frameNum"));
                        frame.setPosition_x(getIntValue(ea, "x"));
                        frame.setPosition_y(getIntValue(ea, "y"));
                        track.getKeyframes().add(frame);
                    }
                }
            }

            track.setName(name);
            track.setWidth(width);
            track.setHeight(height);

            System.out.println(track.toString());
            tracks.add(track);
        }

        return tracks;
    }

    public ArrayList<Image> getImages() {
        ArrayList<Image> images = new ArrayList<Image>();

        NodeList nodes = doc.getElementsByTagName("image");
        for (int i = 0; i < nodes.getLength(); i++) {
            Image image = new Image();

            Element e = (Element) nodes.item(i);
            String fileName = getTextValue(e, "filename");
            String filepath = getTextValue(e, "filepath");
            int width = getIntValue(e, "width");
            int height = getIntValue(e, "height");
            int owidth = getIntValue(e, "originalWidth");
            int oheight = getIntValue(e, "originalHeight");
            int x = getIntValue(e, "positionx");
            int y = getIntValue(e, "positiony");

            NodeList actionsFather = e.getElementsByTagName("actions");
            if (actionsFather != null && actionsFather.getLength() > 0) {
                Element e2 = (Element) actionsFather.item(0);
                NodeList actions = e2.getElementsByTagName("action");
                if (actions != null && actions.getLength() > 0) {
                    for (int j = 0; j < actions.getLength(); j++) {
                        Action action = new Action();
                        Element ea = (Element) actions.item(j);
                        int actionNumber = getIntValue(ea, "actionNum");
                        action.setActionNumber(actionNumber);
                        String param;
                        String param2;
                        switch (actionNumber) {
                            case 7:
                                param = getTextValue(ea, "param1");
                                action.setParam1(param);
                                break;
                            case 8:
                                param = getTextValue(ea, "param1");
                                param2 = getTextValue(ea, "param2");
                                action.setParam1(param);
                                action.setParam2(param2);
                                image.setStartFrame(Integer.parseInt(param));
                                image.setEndFrame(Integer.parseInt(param2));
                                break;
                            default:
                        }
                        image.getActions().add(action);
                    }
                }
            }

            image.setFilename(fileName);
            image.setFilepath(filepath);
            image.setWidth(owidth);
            image.setHeight(oheight);
            image.setResize_x(width);
            image.setResize_y(height);
            image.setX(x);
            image.setY(y);

            images.add(image);
        }
        return images;
    }

    public Video getVideo() {
        Video video = new Video();

        NodeList videoLists = doc.getElementsByTagName("video");

        if (videoLists != null && videoLists.getLength() > 0) {

            Element e = (Element) videoLists.item(0);

            String filepath = getTextValue(e, "filepath");
            String fileName = getTextValue(e, "filename");
            int width = getIntValue(e, "width");
            int height = getIntValue(e, "height");
            int owidth = getIntValue(e, "originalWidth");
            int oheight = getIntValue(e, "originalHeight");
            int x = getIntValue(e, "positionx");
            int y = getIntValue(e, "positiony");

            NodeList actionsFather = e.getElementsByTagName("actions");
            if (actionsFather != null && actionsFather.getLength() > 0) {
                Element e2 = (Element) actionsFather.item(0);
                NodeList actions = e2.getElementsByTagName("action");

                if (actions != null && actions.getLength() > 0) {
                    for (int j = 0; j < actions.getLength(); j++) {
                        Action action = new Action();
                        Element ea = (Element) actions.item(j);
                        int actionNumber = getIntValue(ea, "actionNum");
                        action.setActionNumber(actionNumber);
                        String param;
                        String param2;
                        switch (actionNumber) {
                            case 7:
                                param = getTextValue(ea, "param1");
                                action.setParam1(param);
                                break;
                            case 8:
                                param = getTextValue(ea, "param1");
                                param2 = getTextValue(ea, "param2");
                                action.setParam1(param);
                                action.setParam2(param2);
                                break;
                            default:
                        }
                        video.getActions().add(action);
                    }
                }
            }


            video.setFilename(fileName);
            video.setFilepath(filepath);
            video.setWidth(owidth);
            video.setHeight(oheight);
            video.setResize_x(width);
            video.setResize_y(height);
            video.setX(x);
            video.setY(y);
        }
        return video;
    }

    public Audio getAudio() {
        Audio audio = new Audio();

        NodeList audioLists = doc.getElementsByTagName("audio");

        if (audioLists != null && audioLists.getLength() > 0) {
            Element e = (Element) audioLists.item(0);

            String fileName = getTextValue(e, "filename");
            String filepath = getTextValue(e, "filepath");

            audio.setFilename(fileName);
            audio.setFilepath(filepath);
        }
        return audio;
    }

    public String getTextValue(Element e, String name) {
        String val = null;
        NodeList nodes = e.getElementsByTagName(name);
        if (nodes != null && nodes.getLength() > 0) {
            Element el = (Element) nodes.item(0);
            val = el.getFirstChild().getNodeValue();
        }
        return val;
    }

    public int getIntValue(Element e, String name) {
        return Integer.parseInt(getTextValue(e, name));
    }

    public static void main(String[] args) {
        XMLReader reader = new XMLReader("sample1.my576", "/Users/haoxu/Desktop/final_sample.my576");
        reader.init();
        reader.getTracks();
    }
}
